package sysRecmd;

import java.util.ArrayList;

import main.SYRRES;
import core.Review;
import core.SetBeer;
import core.SetReview;
import core.SetUser;

public class SysRecmd {
	private ArrayList<Review> allReview;
	private SetReview train = new SetReview();
    private SetReview test = new SetReview();
    private SetReview ignore = new SetReview();
    
    private int noteID = 4;
    private int numberOfConvergence = 5000;
    private int thresholdOfConvergence = 1000;
    
	public SysRecmd(ArrayList<Review> allReview) {
		this.setAllReview(allReview);	    
		
//		filterReviews();
		
		// Decouper les revues en 2 groupes : Train et Test
		int i = 0;
	    for(Review r : allReview){
	        if (i < 50000)
	            train.addReview(r);
	        else
	            test.addReview(r);
	        i++;            
	    }
	}  
	
	public void filterReviews() {
		boolean dup = false;
    	for (int i = 0; i < allReview.size(); i++) {
    		if (i % 1000 == 0) System.out.println(i + "/" + allReview.size());
    		dup = false;
    		for (int j = i + 1; j < allReview.size(); j++) {
    			if (allReview.get(i).isSameReviewButDifNote(allReview.get(j), noteID)) {
    				/*for (int x = 0; x < allReview.get(i).getContent().size(); x++) {
    					wFinal[(int)trainReview.getReviews().get(i).getContent().keySet().toArray()[x]] = true;
    				}*/
                    
//                    ignore.addReviewIfNotExists(allReview.get(i));
//                    ignore.addReviewIfNotExists(allReview.get(j));
                    dup = true;
                    allReview.remove(allReview.get(j));
                    j--;
    			}  
    		}              
    		if (dup) {
    			allReview.remove(allReview.get(i));
    			i--;
    		}
    	}      
//    	allReview.removeAll(ignore.getReviews());
    }
	
	public void trainPerceptron() {
	    Perceptron perceptron = new Perceptron(train, test, noteID, numberOfConvergence, thresholdOfConvergence);
	    perceptron.trainReviews();
	    perceptron.getPositive_NegativeWords(50, false);
	}
	
	public void doMatrixFactorization(SetBeer allBeer, SetUser allUser,
										boolean withRegularisation, boolean withBiais) {
		Recommandation recommandation = new Recommandation(train, test, 
															noteID, numberOfConvergence, thresholdOfConvergence,
															withRegularisation, withBiais);
		recommandation.doMatrixFactorization(SYRRES.HINGE_LOSS);
	}

	public ArrayList<Review> getAllReview() {
		return allReview;
	}

	public void setAllReview(ArrayList<Review> allReview) {
		this.allReview = allReview;
	}
}
